
#ifndef _MC31xx_HAL_MUX_H
#define _MC31xx_HAL_MUX_H

#include "mc31xx_hal_def.h"



#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif /* __cplusplus */
#endif /* __cplusplus */




//互斥锁状态
#define HAL_MUX_UNUSED                   0
#define HAL_MUX_USED                     1



//通过互斥锁id获取对应结构体指针
#define __HAL_GET_MUX(muxid)                    ((MuxCB_S*)&g_stAllMux[muxid])

//
#define __HAL_GET_MUX_FROM_LIST(ptr)            HAL_DL_LIST_ENTRY(ptr, MuxCB_S, list)

//向审核线程申请使用互斥锁模块
#define __HAL_LOCK_MUX()            do { \
                                        g_stAllThread[THREAD_ID].lock_mux = 1;\
                                        while (g_stAllThread[THREAD_ID].allow_mux == 0) {}\
                                    } while(0U)

//通知审核线程互斥锁使用完毕
#define __HAL_UNLOCK_MUX()          do { \
                                        g_stAllThread[THREAD_ID].lock_mux = 0;\
                                    } while(0U)

//错误码 暂定
//互斥锁不可用
#define HAL_ERRNO_MUX_INVALID           1
//空指针
#define HAL_ERRNO_MUX_PTR_NULL          2
//没有可用的互斥锁
#define HAL_ERRNO_MUX_ALL_BUSY          3
//互斥锁无法在非阻塞模式下锁定，因为它被另一个线程锁定
#define HAL_ERRNO_MUX_UNAVAILABLE       4
//互斥锁超时
#define HAL_ERRNO_MUX_TIMEOUT           5
//无法删除正在锁定的互斥锁
#define HAL_ERRNO_MUX_PENDED            6


//互斥锁模块初始化
extern uint32_t HAL_MUX_Init(void);

//创建互斥锁 
//puwMuxHandle为指向存放互斥锁handle的指针
//成功返回HAL_OK
extern uint32_t HAL_MUX_Create(uint32_t* puwMuxHandle);

//删除互斥锁
//成功返回HAL_OK
extern uint32_t HAL_MUX_Delete(uint32_t puwMuxHandle);

//申请互斥锁
//允许递归锁
//成功返回HAL_OK
extern uint32_t HAL_MUX_Pend(uint32_t uwMuxHandle, uint32_t uwTimeout);

//释放互斥锁
//成功返回HAL_OK
extern uint32_t HAL_MUX_Post(uint32_t uwMuxHandle);


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

#endif 
